package com.leet.bfs;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class LevelTravesal {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ans = new ArrayList<>();
        Queue<TreeNode> q = new LinkedList<>();
        List<Integer> tmp = new ArrayList<>();
        if(root == null){
            return ans;
        }
        q.offer(root);
        while (!q.isEmpty()){
            int size = q.size();
            tmp.clear();
            for(int i = 0; i < size;i++){
                TreeNode front = q.peek();
                tmp.add(front.val);
                if(front.left != null) q.offer(front.left);
                if(front.right != null) q.offer(front.right);
                q.poll();
            }
            ans.add(tmp);
        }
        return ans;
    }
}
